package com.bio.jsf;

import com.bio.jpa.entities.Status;
import com.bio.jsf.util.JsfUtil;
import com.bio.jpa.session.StatusFacade;

import java.io.Serializable;
import java.util.List;
import java.util.ResourceBundle;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

@ManagedBean(name = "statusController")
@SessionScoped
public class StatusController implements Serializable {

    private Status selected;
    private List<Status> items = null;
    @EJB
    private StatusFacade ejbFacade;

    public StatusController() {
    }

    public List<Status> getItems() {
        items = getFacade().findAll();
        return items;
    }

    public void setItems(List<Status> items) {
        this.items = items;
    }

    public Status getCurrent() {
        if (selected == null) {
            selected = new Status();
        }
        return selected;
    }

    public void setCurrent(Status current) {
        this.selected = current;
    }

    private StatusFacade getFacade() {
        return ejbFacade;
    }

    public String prepareList() {
        return "List";
    }

    public String prepareView() {
        return "View";
    }

    public String prepareCreate() {
        selected = new Status();
        return "Create";
    }

    public void resetLists() {
        items = null;
        selected = null;
    }

    public String create() {
        try {
            getFacade().create(selected);
            JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("StatusCreated"));
            resetLists();
            return "List";
        } catch (Exception e) {
            JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
            return null;
        }
    }

    public String prepareEdit() {
        return "Edit";
    }

    public String update() {
        try {
            getFacade().edit(selected);
            JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("StatusUpdated"));
            return "View";
        } catch (Exception e) {
            JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
            return null;
        }
    }

    public String destroy() {
        performDestroy();
        return "";
    }

    public String destroyAndView() {
        performDestroy();
        return "List";
    }

    private void performDestroy() {
        try {
            getFacade().remove(selected);
            selected = null;
            JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("StatusDeleted"));
            resetLists();
        } catch (Exception e) {
            JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
        }
    }

    @FacesConverter(forClass = Status.class)
    public static class StatusControllerConverter implements Converter {

        public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
            if (value == null || value.length() == 0) {
                return null;
            }
            StatusController controller = (StatusController) facesContext.getApplication().getELResolver().
                    getValue(facesContext.getELContext(), null, "statusController");
            return controller.ejbFacade.find(getKey(value));
        }

        java.lang.Integer getKey(String value) {
            java.lang.Integer key;
            key = Integer.valueOf(value);
            return key;
        }

        String getStringKey(java.lang.Integer value) {
            StringBuffer sb = new StringBuffer();
            sb.append(value);
            return sb.toString();
        }

        public String getAsString(FacesContext facesContext, UIComponent component, Object object) {
            if (object == null) {
                return null;
            }
            if (object instanceof Status) {
                Status o = (Status) object;
                return getStringKey(o.getStsId());
            } else {
                throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: " + StatusController.class.getName());
            }
        }
    }
}
